package com.dxy.app.widget.shading_view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
  *Created by duxingyu on 2017/7/26.14:53
  *Email: 
  *Description:仿微信切换界面逐渐变色的item
  **/

public class ShadingItemView extends View {

  public static final String TAG="TAG_ShadingItemView";

  private String tagName;
  private int width,height;
  private Bitmap bitmapDef,bitmapSel;
  public static final int MARGIN_TOP =5;
  private int marginTop;
  public ShadingItemView(Context context) {
    super(context);
  }

  public ShadingItemView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }
  public ShadingItemView(Context context,String tabName,int defResId,int selResId){
    super(context);
    this.tagName =tabName;

    Resources res =context.getResources();
    bitmapDef = BitmapFactory.decodeResource(res,defResId);
    bitmapSel = BitmapFactory.decodeResource(res,selResId);

    init();
  }
  public ShadingItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  private float textWidth;
  private int bitmapWidth,bitmapHeight;

  private int curAlpha =0;
  private void init(){
    float density =getContext().getResources().getDisplayMetrics().scaledDensity;

    paintDef =new Paint();
    paintSel =new Paint();
    paintTab =new TextPaint();
    paintTab.setTextSize(10*density);

    setPaintSetting(paintDef,paintSel,paintTab);

    bitmapHeight =height =bitmapDef.getHeight();
    bitmapWidth =bitmapDef.getWidth();

    textWidth =paintTab.measureText(tagName);
    float textHeight =paintTab.descent()-paintTab.ascent();

    marginTop = (int) (MARGIN_TOP*density);

    height+=textHeight*2+marginTop;


    Log.i(TAG, "init: "+height);
  }
  /**
   * 绘制图片的区域
   * */
  private Rect rect;

  private void setPaintSetting(Paint...paints){
    for(Paint paint:paints){
      paint.setAntiAlias(true);
    }
  }

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width =MeasureSpec.getSize(widthMeasureSpec);
    setMeasuredDimension(width,height);
  }

  private Paint paintDef,paintSel;
  private TextPaint paintTab;

  @Override protected void onDraw(Canvas canvas) {


    if(rect==null){
      rect =new Rect((width-bitmapWidth)/2,marginTop, (width-bitmapWidth)/2+bitmapWidth,marginTop+bitmapHeight);
    }
    paintDef.setAlpha(curAlpha);
    canvas.drawBitmap(bitmapDef,null,rect,paintDef);

    paintSel.setAlpha(255-curAlpha);
    canvas.drawBitmap(bitmapSel,null,rect,paintSel);

    int color =Color.rgb(0, (int) (255*(curAlpha/255f)),0);
    paintTab.setColor(color);
    canvas.drawText(tagName,(width-textWidth)/2f,rect.bottom+paintTab.descent()-paintTab.ascent(),paintTab);
  }
  public void setCurAlpha(int curAlpha){
    this.curAlpha =curAlpha;
    invalidate();
  }
}
